<?php
namespace App\Service\Front;

use Illuminate\Support\Facades\DB;

class DesignCaseService extends BaseService
{
    //设计商城-案例

    protected $workTypes = [ //工作类型
        1 => '兼职', '全职',
    ];
    //学历：0无，1小学，2初中，3高中，4大专，5本科，6研究生，7博士
    protected $educations = [
        '无', '小学', '初中', '高中', '大专', '本科', '研究生', '博士',
    ];

    //选项
    public function getOption()
    {
        //分类，地区，工作类型，学历
        $arys = ['cate' => [], 'area' => [], 'work_type' => [], 'education' => []];
        //分类选项
        $catePones = $this->cateOption(3, 0);
        if (count($catePones)) {
            foreach ($catePones as $v) {
                $v = (array) $v;
                $arys['cate'][] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'childList' => $this->cateOption(3, $v['id']),
                ];
            }
        }
        //地区联动
        $areaPros = $this->areaOption();
        if (count($areaPros)) {
            foreach ($areaPros as $v) {
                $v = (array) $v;
                $cityList = $this->areaOption($v['id']);
                if (count($cityList)) {
                    foreach ($cityList as $kk => $vv) {
                        $vv = (array) $vv;
                        $cityList[$kk]->childList = $this->areaOption($vv['id']);
                    }
                }
                $arys['area'][] = [
                    'id' => $v['id'],
                    'title' => $v['title'],
                    'childList' => $cityList,
                ];
            }
        }
        //工作类型
        $types = $this->workTypes;
        foreach ($types as $k => $v) {
            $arys['work_type'][] = [
                'id' => $k,
                'title' => $v,
            ];
        }
        //工作类型
        $edus = $this->educations;
        foreach ($edus as $k => $v) {
            $arys['education'][] = [
                'id' => $k,
                'title' => $v,
            ];
        }
        return $arys;
    }

    //获取子分类IDS
    public function getCateids($pid)
    {
        $ids = [(int)$pid];
        $res = $this->cateOption(2, $pid);
        if (!count($res)) {
            return $ids;
        }
        foreach ($res as $v) {
            $ids[] = $v->id;
        }
        return $ids;
    }

    /**
     * 列表
     * @param int $cateid 分类ID
     * @param array $pages【start，limit】
     */
    public function getIndex($cateid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbDesignCase)->where('del', 0);
        if ($cateid) {
            $cateids = $this->getCateids($cateid);
            $db = $db->whereIn('cate_id', $cateids);
        }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            //企业信息
            $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $v['cid'])->first();
            $cname = empty($comOne) ? '-' : $comOne['cname'];
            $clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
            //分类信息
            $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $v['cate_id'])->first();
            $cateName = empty($cateOne) ? '-' : $cateOne['title'];
            $thumb = file_exists($v['thumb']) ? env('DOMAIN').'/'.$v['thumb'] : '';
            $arys[] = [
                'id' => $v['id'],
                'com_id' => $v['cid'],
                'com_name' => $cname,
                'com_logo' => $clogo,
                'title' => $v['title'],
                'cate_id' => $v['cate_id'],
                'cate_name' => $cateName,
                'thumb' => $thumb,
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

//    //一个
//    public function getOne($id)
//    {
//        $res = (array) $this->dbConn($this->dbDesignCase)
//            ->where('del', 0)
//            ->where('id', $id)
//            ->first();
//        if (empty($res)) {
//            return $this->toback(-2, '没有记录');
//        }
//        //企业信息
//        $comOne = (array) $this->dbConn($this->dbCompany)->where('id', $res['cid'])->first();
//        $cname = empty($comOne) ? '-' : $comOne['cname'];
//        //$clogo = !empty($comOne)&&file_exists($comOne['logo']) ? env('DOMAIN').'/'.$comOne['logo'] : '';
//        //分类信息
//        $cateOne = (array) $this->dbConn($this->dbCate)->where('id', $res['cate_id'])->first();
//        $cateName = empty($cateOne) ? '-' : $cateOne['title'];
//        $thumb = file_exists($res['thumb']) ? env('DOMAIN').'/'.$res['thumb'] : '';
//        $arys = [
//            'title' => $res['title'],
//            'com_id' => $res['cid'],
//            'com_name' => $cname,
//            'cate_id' => $res['cate_id'],
//            'cate_name' => $cateName,
//            'thumb' => $thumb,
//        ];
//        return $this->toback(0, '', $arys);
//    }

    //添加
    public function add($pas)
    {
//        //验证分类
//        $cateOne = (array) $this->dbConn($this->dbCate)
//            ->where('id', $pas['cate_id'])
//            ->first();
//        if (empty($cateOne)) {
//            return $this->toback(-2, '分类不存在');
//        }
        //数据入库
        $lastid = $this->dbConn($this->dbDesignCase)->insertGetId($pas);
        if (!$lastid) {
            return $this->toback(-1, '发布失败');
        }
        return $this->toback();
    }
}